﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ETALON
{
    public partial class FormKolZakaz : Form
    {
        public  bool IsSelect;
        public  string valueEnter, valueInb; 
        string v1, v2, v3;
        public double double_sumIn, d_cur, dSdacha;
        public int modeEnter;//1-kol, 2-monySdacha
        string ftext;
        public FormKolZakaz()
        {
            InitializeComponent(); InitializeComponentMy(); setWindowPosition(); modeEnter = 1;
        }
        public FormKolZakaz(string txt) : this()
        {
            this.Text = txt;
        }
        public FormKolZakaz(string txt, string  valueIn)
            : this()
        {
            this.Text = txt; valueEnter = valueIn; valueInb = valueIn; ev = valueEnter;
        }
        public FormKolZakaz(double d0)
            : this()
        {
            double_sumIn = d0; d_cur = 0; dSdacha = 0; modeEnter = 2; ftext = string.Concat("Заказ на сумму: ", double_sumIn); this.Text = ftext;
        }
        private string ev
        {
            get
            {
                return v1;
            }
            set 
            {
                if (value == valueInb && toolStripTextBox1.Text.Length<2)
                {
                    v1 = value; toolStripTextBox1.Text = ev;
                }
                else { v1 = string.Concat(v1, value); toolStripTextBox1.Text = ev; }

                //v1 = string.Concat(v1, value); toolStripTextBox1.Text = ev;

               // MessageBox.Show(modeEnter.ToString());
                if (modeEnter == 2)
                {
                    d_cur = Convert.ToDouble(ev);
                    dSdacha = d_cur - double_sumIn;
                    this.Text = string.Concat(ftext, " Сдача=", dSdacha);
                }
            }
        }
        public void setWindowPosition()
        {
            Point p1 = Cursor.Position;        this.Left = p1.X-150; this.Top = p1.Y-150;
        }
        private void InitializeComponentMy()
        {
            v1 = ""; v2 = ""; v3 = ""; valueEnter = ""; IsSelect = false; 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ev = "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ev = "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ev = "3";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ev = "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ev = "5";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ev = "6";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            ev = "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ev = "8";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            ev = "9";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            ev = "0";
        } 

        private void button10_Click(object sender, EventArgs e)
        {
            ev = ".";  toolStripTextBox1.Text = ev;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (modeEnter == 2 && dSdacha < 0)
            {
                    MessageBox.Show(string.Concat("Сдача = ", dSdacha, " меньше нуля. Внесите правильно сумму.")); IsSelect = false;
            }
            else { IsSelect = true; this.Close(); }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string v; int le=0;
            if (v1.Contains('.'))
            {
                string[] a = v1.Split('.');  le = a[1].Length;
                if (le > 1)
                {
                    v = string.Concat(a[0], ".", a[1].Substring(0, le - 1));   v1 = v;
                }
                else
                {
                    v = a[0]; v1 = v;  //v3 = "";
                }
            }
            else 
            { 
                le = v1.Length;
                if (le > 1) v1 = v1.Substring(0, le - 1);
                else if (le == 1) v1 = ""; 
            }
            toolStripTextBox1.Text = ev;
            if (toolStripTextBox1.Text.Length > 0)
            {
                d_cur = Convert.ToDouble(toolStripTextBox1.Text);
                dSdacha = d_cur - double_sumIn;
                this.Text = string.Concat(ftext, " Сдача=", dSdacha);
            }
            else { this.Text = string.Concat(ftext, " Сдача=", 0); }
        }

        private void FormKolZakaz_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (IsSelect) valueEnter = toolStripTextBox1.Text; else valueEnter = "";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            IsSelect = false; this.Close();
        }
    }
}
